package cn.rkylin.oms.item.adapter;

import cn.rkylin.oms.item.request.DelistingItemRequest;
import cn.rkylin.oms.item.request.ListingItemRequest;
import cn.rkylin.oms.push.adapter.PushAdapter;
import cn.rkylin.oms.push.adapter.PushAdapterFactory;
import cn.rkylin.oms.system.shop.domain.Shop;
import cn.rkylin.oms.system.shop.service.IShopService;
import cn.rkylin.oms.system.shop.vo.ShopVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.Map;

public abstract class ItemAdapter {

    /**
     * 店铺服务层
     */
    @Autowired
    private IShopService shopService;

    /**
     * 同步店铺库存
     *
     * @param shopId
     * @return
     * @throws Exception
     */
    public abstract String synchShopStock(String shopId) throws Exception;

    /**
     * 同步商品库存
     *
     * @param ecItemId
     * @return
     * @throws Exception
     */
    public abstract String synchItemStock(String shopId, String ecItemId) throws Exception;

    /**
     * 同步sku库存
     *
     * @param ecSkuId
     * @return
     * @throws Exception
     */
    public abstract String synchSkuStock(String shopId, String ecSkuId) throws Exception;


    /**
     * 商品上架
     *
     * @param shopId
     * @param ecItemCode
     * @param stock
     * @return
     * @throws Exception
     */
    public abstract String listingItem(String shopId, ListingItemRequest listingItemRequest) throws Exception;

    /**
     * 商品下架
     *
     * @param shopId
     * @param delistingItemRequest
     * @return
     * @throws Exception
     */
    public abstract String delistingItem(String shopId, DelistingItemRequest delistingItemRequest) throws Exception;


    /**
     * 请求参数验证
     *
     * @throws Exception
     */
    protected Map<String, Object> pushDataValidate(String shopId) throws Exception {
        Map<String, Object> resultMap = new HashMap<>();
        if (StringUtils.isEmpty(shopId)) {
            resultMap.put("success", "n");
            resultMap.put("msg", "店铺不存在");
            return resultMap;
        }
        ShopVO shop = shopService.getShopById(shopId);
        if (shop == null || StringUtils.isEmpty(shop.getEnable()) || StringUtils.isEmpty(shop.getDeleted())) {
            resultMap.put("success", "n");
            resultMap.put("msg", "店铺不存在");
            return resultMap;
        } else if (shop.getEnable().equalsIgnoreCase("n")) {
            resultMap.put("success", "n");
            resultMap.put("msg", "店铺未启用");
            return resultMap;
        } else if (shop.getDeleted().equalsIgnoreCase("y")) {
            resultMap.put("success", "n");
            resultMap.put("msg", "店铺已删除");
            return resultMap;
        }
        // 如果是子店铺
        if (StringUtils.isNotEmpty(shop.getParentShop())) {
            Shop shopParent = shopService.getShopById(shop.getParentShop());
            shop.setApiUrl(shopParent.getApiUrl());
            shop.setAppKey(shopParent.getAppKey());
            shop.setAppSecret(shopParent.getAppSecret());
            shop.setSessionKey(shopParent.getSessionKey());
        }
        resultMap.put("success", "y");
        resultMap.put("msg", "");
        resultMap.put("shop", shop);
        return resultMap;
    }
}
